<?php

namespace App\Http\Controllers\admin_system;

use App\Models\V2\AccountsManageModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\V2\CompanyModel;

class AccountsManageController extends Controller
{

    /**
     * 管理员列表
     * */
    public function index(Request $request)
    {
        $keywords   = trim($request->input('keywords',''));
        $accountsManageModel = new AccountsManageModel();
        $data       = $accountsManageModel->getCompanyManagerUserInfo($keywords);
        return view('admin_system.accountsManage.index',['data'=>$data,'keywords'=>$keywords]);
    }


    /**
     * 添加管理员界面
     * */
    public function account_add(Request $request)
    {
        $companyId      = $request->Input('companyId');
        $companyModel   = new CompanyModel();
        $companyInfo    = $companyModel->get_company_info($companyId);
        return view('admin_system.accountsManage.account_add', ['company'=>$companyInfo]);
    }





    public function base_info(AccountsManageModel $accountsManageModel, Request $request)
    {
        $company_code = $request->Input('company_code');

        $data           = $accountsManageModel->getCompanyManagerUserInfo();
        $unique_company = [];

        array_walk($data, function ($item) use ($company_code, &$unique_company) {
            if ($item['company_sn'] == $company_code) {
                $unique_company = $item;
            }
        });

        return view('admin_system.accountsManage.base_info', compact('unique_company', 'company_code'));
    }

    public function base_info_edit(AccountsManageModel $accountsManageModel, Request $request)
    {
        $company_name   = $request->Input('company_name');
        $company_sn     = $request->Input('company_sn');
        $company_start  = $request->Input('company_start');
        $company_end    = $request->Input('company_end');
        $manage_name    = $request->Input('manage_name');
        $manage_account = $request->Input('manage_account');
        $password       = $request->Input('password');

        $data   = compact('company_name', 'company_sn', 'company_start', 'company_end', 'manage_name', 'manage_account', 'password');
        $status = $accountsManageModel->updateAdminUsersAndCompany($data);
        if ($status) {
            return redirect()->back()->with(['account_success' => '更新成功']);
        }

        return redirect()->back()->with(['account_errors' => '更新失败']);
    }

    public function son_manage(AccountsManageModel $accountsManageModel, Request $request)
    {
        $company_code = $request->Input('company_code');

        $data           = $accountsManageModel->getCompanyManagerUserInfo();
        $unique_company = [];

        array_walk($data, function ($item) use ($company_code, &$unique_company) {
            if ($item['company_sn'] == $company_code) {
                $unique_company = $item;
            }
        });

        $son_company = $accountsManageModel->getCompnySonAccount($company_code);


        return view('admin_system.accountsManage.son_manage', compact('unique_company', 'company_code', 'son_company'));
    }

    public function son_manage_edit(AccountsManageModel $accountsManageModel, Request $request)
    {
        $data   = $request->all();
        $status = $accountsManageModel->updateAdminUsers($data);
        if ($status) {
            return redirect()->back()->with(['account_success' => '更新成功']);
        }

        return redirect()->back()->with(['account_errors' => '更新失败']);
    }
}
























